home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / NMMAN.H < prev    next >
C/C++ Source or Header  |  1992-03-03  |  5KB  |  163 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* nmman.h */
  21. /* New memory manager internal definitions for Ghostscript */
  22.  
  23. /* ====== Allocator ====== */
  24.  
  25. /* Define the alignment modulus for aligned objects. */
  26. #define log2_align_mod 3        /* big enough for a double */
  27.                     /* and for a struct_header */
  28. #define align_mod (1 << log2_align_mod)
  29. #define align_mask (align_mod-1)
  30. #define align_round(siz) (uint)(((siz) + align_mask) & -align_mod)
  31.  
  32. /* Structure header. */
  33. /* There are 3 different formats, as indicated below. */
  34. /* Note that we force the size of a structure header to be align_mod. */
  35. typedef union struct_header_s struct_header_t;
  36. union struct_header_s {
  37.   struct {
  38.     unsigned mark : 1, large : 1, bytes : 1;
  39.   } h;                    /* all formats */
  40.     /* Ordinary typed structure */
  41.   struct {
  42.     unsigned _ : 3, reloc8 : 13;
  43.     at_ptr_t type;
  44.   } s;                    /* large = 0, bytes = 0 */
  45.     /* Plain (but aligned) bytes */
  46.   struct {
  47.     unsigned _ : 3, reloc8 : 13;
  48.     ushort size;
  49.   } b;                    /* large = 0, bytes = 1 */
  50.     /* Large bytes (in a chunk by themselves) */
  51.   struct {
  52.     unsigned _ : 3, lsize : 13;
  53.     ushort size;
  54.   } l;                    /* large = 1, bytes = 1 */
  55.     /* Force size up to align_mod */
  56.   byte _[align_mod];
  57. };
  58. #define struct_size(shp)\
  59.   ((shp)->h.bytes ? (shp)->b.size : (shp)->s.type->size)
  60. #define struct_large_size(shp)\
  61.   (((ulong)(shp)->l.lsize << 16) + (shp)->l.size)
  62. #define struct_next(shp)\
  63.   ((struct_header_t *)((byte *)(shp) + struct_size(shp)))
  64.  
  65. /* ====== Chunks ====== */
  66.  
  67. /* Chunks are "objects", i.e. they have a header that identifies */
  68. /* which implementation is being used. */
  69. typedef struct chunk_s chunk_t;
  70. typedef struct chunk_locator_s chunk_locator_t;
  71. typedef chunk_locator_t _ss *cl_ptr_t;
  72.  
  73. #define declare_chunk_procs(dscope, ctype, init, status, gc_init, gc_trace_from_marked, gc_set_reloc, gc_do_reloc, gc_compact)\
  74. \
  75.         /* Initialize the chunk. */\
  76.     dscope void init(P3(ctype *, byte *, usize_t));\
  77. \
  78.         /* Return the space allocated and used. */\
  79.     dscope void status(P2(ctype *, alloc_status_t *));\
  80. \
  81.     /**** The rest of the procedures are only for the GC. ****/\
  82. \
  83.         /* Initialize for a GC by clearing marks. */\
  84.     dscope void gc_init(P1(ctype *));\
  85. \
  86.         /* Trace from all marked pointers. */\
  87.         /* Return true if any new marks. */\
  88.     dscope bool gc_trace_from_marked(P1(ctype *));\
  89. \
  90.         /* Compute and store relocation amounts. */\
  91.     dscope void gc_set_reloc(P2(ctype *, cl_ptr_t));\
  92. \
  93.         /* Relocate pointers. */\
  94.     dscope void gc_do_reloc(P2(ctype *, cl_ptr_t));\
  95. \
  96.         /* Compact to remove unmarked components. */\
  97.     dscope void gc_compact(P1(ctype *))
  98.  
  99. #define chunk_procs_struct(ctype)\
  100.   struct {\
  101.     declare_chunk_procs(, ctype, (*init), (*status), (*gc_init), (*gc_trace_from_marked), (*gc_set_reloc), (*gc_do_reloc), (*gc_compact));\
  102.   }
  103. typedef chunk_procs_struct(chunk_t) chunk_procs_t;
  104. #define chunk_common\
  105.   chunk_procs_t _ds *procs;\
  106.   byte *cbot, *ctop;\
  107.   chunk_t *cprev, *cnext        /* sorted by address */
  108. struct chunk_s {
  109.   chunk_common;
  110. };
  111.  
  112. /* Find the chunk for a pointer. */
  113. #define ptr_is_in_chunk(ptr, cp)\
  114.   ptr_between((byte *)ptr, (cp)->cbot, (cp)->ctop)
  115. struct chunk_locator_s {
  116.   chunk_t *cp;                /* one-element cache */
  117. };
  118. extern bool chunk_locate_ptr(P2(byte *, cl_ptr_t));
  119. #define chunk_locate(ptr, clp)\
  120.   (ptr_is_in_chunk((byte *)(ptr), (clp)->cp) ||\
  121.    chunk_locate_ptr((byte *)(ptr), clp))
  122.  
  123. /* ------ Concrete chunks ------ */
  124.  
  125. /* Many small structures. */
  126. typedef struct {
  127.   chunk_common;
  128.   struct_header_t *top;
  129. } chunk_structs_t;
  130.  
  131. /* One large structure. */
  132. typedef struct {
  133.   chunk_common;
  134. } chunk_large_struct_t;
  135.  
  136. /* Refs and strings. */
  137. typedef struct {
  138.   chunk_common;
  139.   struct ref_s *rtop;
  140.   byte *ibot, *itop;
  141.   /* Rest is for GC */
  142.   byte *ibits;
  143.   byte *ibase;
  144.   usize_t ibitsize;
  145.   ushort *ireloc;
  146.   uint imove;
  147. } chunk_refs_t;
  148.  
  149. /* ====== Definition of allocator state ====== */
  150.  
  151. typedef struct std_alloc_state_s std_alloc_state_t;
  152. struct std_alloc_state_s {
  153.   alloc_state_common;
  154.   chunk_structs_t ccs;            /* current structs chunk */
  155.   chunk_structs_t *pccs;        /* where to put ccs */
  156.   chunk_refs_t ccrs;            /* current refs chunk */
  157.   chunk_refs_t *pccrs;            /* where to put ccrs */
  158.   uint chunk_size;
  159.   uint large_size;            /* min size for large chunk */
  160.   gc_root_t *roots;
  161.   chunk_t chunk_min, chunk_max;        /* head and tail of chunk list */
  162. };
  163.